<?php

# note_flow_query.php

include_once '_portal.php';

output(['success' => false, 'result' => [], 'sql' => '']);

die();

$limit = isset($post->dateRange) && isset($post->dateRange) ? '' : ' LIMIT 1';
$from = isset($post->dateRange) ? $post->dateRange[0] : date('Y-m-d H:i:s', time() - 300);
$to = isset($post->dateRange) ? $post->dateRange[1] : date('Y-m-d H:i:s');
$advice = isset($post->advice) ? $post->advice : '<999';
$lowRateZUUU = isset($post->rateRangeZUUU) ? $post->rateRangeZUUU[0] : 0;
$highRateZUUU = isset($post->rateRangeZUUU) ? $post->rateRangeZUUU[1] : 100;
$lowRateZUTF = isset($post->rateRangeZUTF) ? $post->rateRangeZUTF[0] : 0;
$highRateZUTF = isset($post->rateRangeZUTF) ? $post->rateRangeZUTF[1] : 100;

$sql = "SELECT * FROM `2kt_note_flow` WHERE (`moment`>= '{$from}' AND `moment`<= '{$to}') OR (`updated`>= '{$from}' AND `updated`<= '{$to}') AND (`manualDifference`{$advice} OR `adviceDifference`{$advice} ) AND `rateZUUU`>={$lowRateZUUU} AND `rateZUUU`<={$highRateZUUU} AND `rateZUTF`>={$lowRateZUTF} AND `rateZUTF`<={$highRateZUTF} {$limit}";

$result = exec_sql($sql);

$result = $result ? query_parse($result) : null;

output(['success' => $result ? true : false, 'result' => $result, 'sql' => $sql]);

/*
{
"dateRange": [
"2023-11-14 00:00:00",
"2023-11-14 23:59:59"
],
"advice": ">0",
"rateRangeZUUU": [
"0",
"100"
],
"rateRangeZUTF": [
"0",
"100"
]
}
 */